﻿#region 참조 네임스페이스

using System;
using System.Globalization;
using System.Text;

#endregion

namespace Skying
{
	public static class StringExtensions
	{
		public static string EscapeString(this string text)
		{
			StringBuilder stringBuilder = new StringBuilder();

			//stringBuilder.Append('"');

			foreach (char character in text)
			{
				if (character.Equals('\"'))
				{
					stringBuilder.Append("\\\"");
				}
				else if (character.Equals('\\'))
				{
					stringBuilder.Append("\\\\");
				}
				else if (character.Equals('\b'))
				{
					stringBuilder.Append("\\b");
				}
				else if (character.Equals('\f'))
				{
					stringBuilder.Append("\\f");
				}
				else if (character.Equals('\n'))
				{
					stringBuilder.Append("\\n");
				}
				else if (character.Equals('\r'))
				{
					stringBuilder.Append("\\r");
				}
				else if (character.Equals('\t'))
				{
					stringBuilder.Append("\\t");
				}
				else if (char.IsLetterOrDigit(character))
				{
					stringBuilder.Append(character);
				}
				else if (char.IsPunctuation(character))
				{
					stringBuilder.Append(character);
				}
				else if (char.IsSeparator(character))
				{
					stringBuilder.Append(character);
				}
				else if (char.IsWhiteSpace(character))
				{
					stringBuilder.Append(character);
				}
				else if (char.IsSymbol(character))
				{
					stringBuilder.Append(character);
				}
				else
				{
					stringBuilder.Append(string.Format("\\u{0}", ((int)character).ToString("x4")));
				}
			}

			//stringBuilder.Append('"');

			return stringBuilder.ToString();
		}

		public static string UnescapeString(string text)
		{
			text = text.Trim();

			if (text.StartsWith("\""))
			{
				text = text.Remove(0, 1);
			}

			if (text.EndsWith("\""))
			{
				text = text.Remove(text.Length - 1, 1);
			}

			StringBuilder stringBuilder = new StringBuilder();

			for (int index = 0; index < text.Length; index++)
			{
				char character = text[index];

				switch (character)
				{
					case '\\':

						index++;

						if (text[index].Equals('u') || text[index].Equals('U'))
						{
							string hex = text.Substring(index + 1, 4);

							char uCharacter = (char)int.Parse(hex, NumberStyles.HexNumber);

							index += 4;

							stringBuilder.Append(uCharacter);

							continue;
						}

						if (text[index].Equals('n'))
						{
							stringBuilder.Append('\n');

							continue;
						}

						if (text[index].Equals('r'))
						{
							stringBuilder.Append('\r');

							continue;
						}

						if (text[index].Equals('t'))
						{
							stringBuilder.Append('\t');

							continue;
						}

						if (text[index].Equals('f'))
						{
							stringBuilder.Append('\f');

							continue;
						}

						if (text[index].Equals('b'))
						{
							stringBuilder.Append('\b');

							continue;
						}

						if (text[index].Equals('\\'))
						{
							stringBuilder.Append('\\');

							continue;
						}

						if (text[index].Equals('/'))
						{
							stringBuilder.Append('/');

							continue;
						}

						if (text[index].Equals('"'))
						{
							stringBuilder.Append('"');

							continue;
						}

						throw new FormatException("");

					default:

						stringBuilder.Append(character);

						break;
				}
			}

			return stringBuilder.ToString();
		}
	}
}